package com.ruoyi.rsikManage.service.Impl;

import java.util.List;


import com.ruoyi.rsikManage.domain.Algorithm;
import com.ruoyi.rsikManage.domain.MonitorPoint;
import com.ruoyi.rsikManage.domain.TVideoStandPlace;
import com.ruoyi.rsikManage.domain.PlaceCategory;
import com.ruoyi.rsikManage.mapper.MonitorPointMapper;
import com.ruoyi.rsikManage.mapper.TVideoStandPlaceMapper;
import com.ruoyi.rsikManage.service.ITVideoStandPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 视频场所标准字典Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@Service
public class TVideoStandPlaceServiceImpl implements ITVideoStandPlaceService
{
    @Autowired
    private TVideoStandPlaceMapper tVideoStandPlaceMapper;
    @Autowired
    private MonitorPointMapper monitorPointMapper;


    @Override
    public boolean checkHasMonitorRelPlace(String placeCategoryDicVal, String monitorId) {
        int count = tVideoStandPlaceMapper.checkHasMonitorRelPlace(placeCategoryDicVal, monitorId);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkHasMonitorRelPlaceForUnitCodes(String placeCategoryDicVal, String monitorId,Long unitCode) {
        int count = tVideoStandPlaceMapper.checkHasMonitorRelPlaceForUnitCodes(placeCategoryDicVal, monitorId,unitCode);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<PlaceCategory> selectPlaceCategoryDicList(String dicType, String parentDicVal) {
        return tVideoStandPlaceMapper.selectPlaceCategoryDicList(dicType, parentDicVal);
    }

    /**
     * 查询视频场所标准字典
     *
     * @param id 视频场所标准字典ID
     * @return 视频场所标准字典
     */
    @Override
    public TVideoStandPlace selectTVideoStandPlaceById(Long id)
    {
        return tVideoStandPlaceMapper.selectTVideoStandPlaceById(id);
    }

    /**
     * 查询视频场所标准字典列表
     *
     * @param tVideoStandPlace 视频场所标准字典
     * @return 视频场所标准字典
     */
    @Override
    public List<TVideoStandPlace> selectTVideoStandPlaceList(TVideoStandPlace tVideoStandPlace)
    {
        return tVideoStandPlaceMapper.selectTVideoStandPlaceList(tVideoStandPlace);
    }

    @Override
    public List<TVideoStandPlace> selectPlaceListWithMonitors(TVideoStandPlace tVideoStandPlace) {
        return tVideoStandPlaceMapper.selectPlaceListWithMonitors(tVideoStandPlace);
    }

    @Override
    public List<TVideoStandPlace> selectPlaceListWithMonitorsForUnitCodes(TVideoStandPlace tVideoStandPlac) {
        return tVideoStandPlaceMapper.selectPlaceListWithMonitorsForUnitCodes(tVideoStandPlac);
    }


    @Override
    public void addMonitorRelPlace(String placeName, String monitorId) {
        //检查监控地在该场所类别的其他场所是否仍然存在
//        boolean isHasMonitorRelPlace = checkHasMonitorRelPlace(placeParentLevelDicVal, monitorId);
//        if (isHasMonitorRelPlace && !confirmDelFlag) {
//            return false;
//        }
        //tVideoStandPlaceMapper.delMonitorRelAllPlace(monitorId);
        List<TVideoStandPlace> samePlaceList = tVideoStandPlaceMapper.getSamePlace(placeName);
        for (TVideoStandPlace place : samePlaceList) {
            tVideoStandPlaceMapper.addMonitorRelPlace(place.getTVideoParentLevel(), place.getTVideoChildLevel(), place.getId(), monitorId);
        }
    }


    @Override
    public void addMonitorRelPlaceForUnitCodes(String placeName, String monitorId,Long unitCode) {
        //检查监控地在该场所类别的其他场所是否仍然存在
//        boolean isHasMonitorRelPlace = checkHasMonitorRelPlace(placeParentLevelDicVal, monitorId);
//        if (isHasMonitorRelPlace && !confirmDelFlag) {
//            return false;
//        }
        //tVideoStandPlaceMapper.delMonitorRelAllPlace(monitorId);
        List<TVideoStandPlace> samePlaceList = tVideoStandPlaceMapper.getSamePlace(placeName);
        for (TVideoStandPlace place : samePlaceList) {
            tVideoStandPlaceMapper.addMonitorRelPlaceForUnitCodes(place.getTVideoParentLevel(), place.getTVideoChildLevel(), place.getId(), monitorId,unitCode);
        }
    }

    @Override
    public void delMonitorRelPlace(String placeName, String monitorId) {
        List<TVideoStandPlace> samePlaceList = getSamePlace(placeName);
        for (TVideoStandPlace place : samePlaceList) {
            tVideoStandPlaceMapper.delMonitorRelPlace(place.getTVideoParentLevel(), place.getId(), monitorId);
        }

    }

    @Override
    public void delMonitorRelPlaceForUnitCodes(String placeName, String monitorId,Long unitCode) {
        List<TVideoStandPlace> samePlaceList = getSamePlace(placeName);
        for (TVideoStandPlace place : samePlaceList) {
            tVideoStandPlaceMapper.delMonitorRelPlaceForUnitCodes(place.getTVideoParentLevel(), place.getId(), monitorId,unitCode);
        }

    }

    @Override
    public List<TVideoStandPlace> getSamePlace(String placeName) {
        return tVideoStandPlaceMapper.getSamePlace(placeName);
    }

    /**
     * 新增视频场所标准字典
     *
     * @param tVideoStandPlace 视频场所标准字典
     * @return 结果
     */
    @Override
    public int insertTVideoStandPlace(TVideoStandPlace tVideoStandPlace)
    {
        return tVideoStandPlaceMapper.insertTVideoStandPlace(tVideoStandPlace);
    }

    /**
     * 修改视频场所标准字典
     *
     * @param tVideoStandPlace 视频场所标准字典
     * @return 结果
     */
    @Override
    public int updateTVideoStandPlace(TVideoStandPlace tVideoStandPlace)
    {
        return tVideoStandPlaceMapper.updateTVideoStandPlace(tVideoStandPlace);
    }

    /**
     * 批量删除视频场所标准字典
     *
     * @param ids 需要删除的视频场所标准字典ID
     * @return 结果
     */
    @Override
    public int deleteTVideoStandPlaceByIds(Long[] ids)
    {
        if (ids.length != 0) {
            monitorPointMapper.deleteMonitorRelStandPlace(ids);
        }
        return tVideoStandPlaceMapper.deleteTVideoStandPlaceByIds(ids);
    }

    /**
     * 删除视频场所标准字典信息
     *
     * @param id 视频场所标准字典ID
     * @return 结果
     */
    @Override
    public int deleteTVideoStandPlaceById(Long id)
    {
        return tVideoStandPlaceMapper.deleteTVideoStandPlaceById(id);
    }

    @Override
    public List<PlaceCategory> selectCategoryListWithPlacesWithMonitors() {
        return tVideoStandPlaceMapper.selectCategoryListWithPlacesWithMonitors();
    }


    @Override
    public List<PlaceCategory> selectCategoryListWithPlace(String standardFile) {
        //1.查询该标准文件下所有的场所类别
        List<PlaceCategory> placeCategoryDicList = selectPlaceCategoryDicList("t_video_child_level", standardFile);
        //2.遍历场所类别列表
        for (PlaceCategory category : placeCategoryDicList) {
            //3.查询该场所类别下的场所
            //构建查询参数
            TVideoStandPlace placeParm = new TVideoStandPlace();
            placeParm.setTVideoParentLevel(standardFile);
            placeParm.setTVideoChildLevel(category.getDicValue());
            List<TVideoStandPlace> placeListInThisCategory = selectTVideoStandPlaceList(placeParm);
            category.setPlaceList(placeListInThisCategory);
        }
        return placeCategoryDicList;
    }

    @Override
    public int delMonitorRelAllPlace(String monitorId) {
        return tVideoStandPlaceMapper.delMonitorRelAllPlace(monitorId);
    }

    @Override
    public int delMonitorRelAllPlaceForUnitCodes(String monitorId,Long unitCode) {
        return tVideoStandPlaceMapper.delMonitorRelAllPlaceForUnitCodes(monitorId,unitCode);
    }
}
